package com.quectel.taskserver.job.patrol;

import com.quectel.business.common.dept.DeptPersonMsgBusiness;
import com.quectel.constant.PushConstants;
import com.quectel.constant.core.dept.DeptPersonMsgConstants;
import com.quectel.constant.core.devicepatrol.DevicePatrolTaskConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.devicepatrol.dto.DevicePatrolTaskDto;
import com.quectel.core.module.devicepatrol.service.DevicePatrolTaskService;
import com.quectel.util.common.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 生成设备巡检任务 每天18点执行
 *
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2022/5/17 20:27 下午
 */
@Component
public class DevicePatrolGenerateTask implements SimpleJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(DevicePatrolGenerateTask.class);

    @DubboReference
    private DevicePatrolTaskService devicePatrolTaskService;
    @DubboReference
    private DeptPersonService deptPersonService;

    @Autowired
    private DeptPersonMsgBusiness deptPersonMsgBusiness;

    @Override
    public void execute(ShardingContext shardingContext) {
        //查询第二天需要创建的计划
        //第二天的日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, 1);
        Date tomorrow = calendar.getTime();
        String dateStr = DateUtils.format(tomorrow, DateUtils.FormatType.SIMPLE);
        LOGGER.info("设备巡检任务-任务日期:{} start", dateStr);
        List<DevicePatrolTaskDto> devicePatrolTaskDtos = devicePatrolTaskService.genDevicePatrolTaskByDate(tomorrow);
        //消息推送
        if (CollectionUtils.isNotEmpty(devicePatrolTaskDtos)) {
            for (DevicePatrolTaskDto dto : devicePatrolTaskDtos) {
                // 发消息通知第二天执行的巡更任务
                DeptPersonDto deptPersonDto = deptPersonService.selectById(dto.getExecUserId());
                if (deptPersonDto != null) {
                    deptPersonMsgBusiness.pushMsg(
                            new ArrayList<>(Collections.singletonList(deptPersonDto)),
                            DeptPersonMsgConstants.SourceType.DEVICE_PATROL_MSG,
                            dto.getId(),
                            null,
                            String.format(DevicePatrolTaskConstants.APP_PUSH_MSG_TEMPLATE, dto.getName(), dto.getDate(), dto.getPeriod()),
                            true,
                            new PushConstants.PushType[]{PushConstants.PushType.APP}
                    );
                }
            }
        }
        LOGGER.info("设备巡检任务-任务日期:{} end", dateStr);
    }
}
